#ifndef EVENTFACTORY_H
#define EVENTFACTORY_H

#include "room_global.h"
#include "../../core/src/log.h"
#include "server.h"
#include <boost/bind.hpp>

class Event;

class EventFactory :
        protected boost::noncopyable
{
public:
    typedef boost::shared_ptr< Event > EventPtr;
    typedef boost::pool<> MemoryPool;

public:
    EventFactory(Server &server);
    ~EventFactory();

    template< typename EventT >
    EventPtr create()
    {
        void *p = m_server.allocMemory(sizeof(EventT) + sizeof(unsigned long));

        if(!p){
            LOG_ERROR() << "Create event: "
                        << strerror(boost::system::errc::not_enough_memory);

            exit(-1);
        }

        unsigned long *len = (unsigned long *)p;

        *len = sizeof(EventT) + sizeof(unsigned long);

        EventPtr _new
                (
                    new(p + sizeof(unsigned long)) EventT(),
                    boost::bind
                    (
                        &EventFactory::destroy,
                        this,
                        _1
                    )
                );

        return _new;
    }

protected:
    void destroy(Event *e);

private:
    Server &m_server;
};

#endif // EVENTFACTORY_H
